package com.recharge.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.recharge.common.domain.PageDO;
import com.recharge.common.enums.ErrorCode;
import com.recharge.common.result.ResultDO;
import com.recharge.common.result.ResultSupport;
import com.recharge.common.util.BeanUtilsExtends;

import com.recharge.core.entity.RegionEntity;

import com.recharge.core.mapper.RegionMapper;
import com.recharge.domain.query.RegionQueryDO;
import com.recharge.domain.vo.RegionDO;

import com.recharge.service.RegionService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author reagan
 * @since 2020-08-19
 */
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, RegionEntity> implements RegionService {

    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());


    public ResultDO add(RegionDO domain) {

        RegionEntity entity = new RegionEntity();

        ResultDO result = BeanUtilsExtends.copy(entity, domain);
        if (!result.isSuccess()) {
            logger.debug("error code：" + result.getErrorCode().getCode() + " msg" + result.getErrorCode().getMsg());
            return result;
        }

        boolean res = false;
        try {
            res = this.save(entity);
        } catch(Exception e) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }

        //判断r的值是否小于0.如果小于0 就代表添加失败
        if(!res) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.REGION_ADD_ERROR);
            logger.debug("error code：" + ErrorCode.REGION_ADD_ERROR.getCode() + " msg" + ErrorCode.REGION_ADD_ERROR.getMsg());
            return result;
        }

        return result;
    }


    public ResultDO edit(RegionDO domain) {

        RegionEntity entity = new RegionEntity();

        ResultDO result = BeanUtilsExtends.copy(entity, domain);
        if (!result.isSuccess()) {
            logger.debug("error code：" + result.getErrorCode().getCode() + " msg" + result.getErrorCode().getMsg());
            return result;
        }

        boolean res = false;
        try {
            res = this.updateById(entity);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }

        //判断r的值是否小于0.如果小于0 就代表添加失败
        if(!res) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.REGION_EDIT_ERROR);
            logger.debug("error code：" + ErrorCode.REGION_EDIT_ERROR.getCode() + " msg" + ErrorCode.REGION_EDIT_ERROR.getMsg());
            return result;
        }

        return result;
    }


    public ResultDO get(String id) {

        ResultDO result = new ResultSupport();
        RegionEntity entity = null;

        try {
            entity = this.getById(id);
        } catch(Exception e) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }

        RegionDO domain = getDomain(entity);

        result.setModel(ResultDO.FIRST_MODEL_KEY, domain);

        return result;
    }


    public ResultDO remove(String id) {
        ResultDO result = new ResultSupport();

        boolean res = false;

        try {
            res = this.removeById(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }

        //判断r的值是否小于0.如果小于0 就代表添加失败
        if(!res) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.REGION_DELETE_ERROR);
            logger.debug("error code：" + ErrorCode.REGION_DELETE_ERROR.getCode() + " msg" + ErrorCode.REGION_DELETE_ERROR.getMsg());
            return result;
        }

        return result;
    }


    public ResultDO find(RegionQueryDO query) {
        RegionEntity entity = new RegionEntity();

        ResultDO result = BeanUtilsExtends.copy(entity,query);

        if (!result.isSuccess()) {
            logger.debug("error code：" + result.getErrorCode().getCode() + " msg" + result.getErrorCode().getMsg());
            return result;
        }

        QueryWrapper<RegionEntity> wrapper = new QueryWrapper<>(entity);

//        Page<RegionEntity> page = new Page<>(query.getCurrentPage(),query.getPageSize());
//        IPage<RegionEntity> iPage = null;

        List<RegionEntity> list = null;

        if(StringUtils.isNotEmpty(query.getId())) {
            wrapper.lambda().eq(RegionEntity::getId, query.getId());
        }

        if(StringUtils.isNotEmpty(query.getRegionId())) {
            wrapper.lambda().eq(RegionEntity::getRegionId, query.getRegionId());
        }

        try {
            list = baseMapper.selectList(wrapper);
        }catch (Exception e) {
            result.setSuccess(false);
            result.setErrorCode(ErrorCode.SYSTEM_EXCEPTION);
            logger.debug("error code：" + ErrorCode.SYSTEM_EXCEPTION.getCode() + " msg" + ErrorCode.SYSTEM_EXCEPTION.getMsg());
            return result;
        }
        List<RegionDO> doList = getDomainList(list);
//        PageDO<RegionDO> pageDO = new PageDO<>();
//
//        pageDO.setCurrentPage(iPage.getCurrent());
//        pageDO.setPageCount(iPage.getPages());
//        pageDO.setPageSize(iPage.getSize());
//        pageDO.setRowCount(iPage.getTotal());
//        pageDO.setData(doList);

        result.setModel(ResultDO.FIRST_MODEL_KEY, doList);
        return result;

    }

    private RegionDO getDomain(RegionEntity entity) {

        //判断房源对象是否为空，为空就返回 空，不为空就进行下面的代码
        if(entity == null) return null;
        RegionDO domain = new RegionDO();

        domain = BeanUtilsExtends.copyProperties(domain, entity) ? domain : null;

        //判断拷贝数据到Dto对象是否成功，如果成功则返回domian，如果不成功就返回空
        return domain;
    }

    private List<RegionDO> getDomainList(List<RegionEntity> list) {
        //创建一个名为dolist的房源do类型的 list集合
        List<RegionDO> doList = new ArrayList<RegionDO>();

        //判断传入的list的集合的size是否大于0
        if(list.size() > 0) {
            //大于0是，遍历list，创建do类型的对象 调用getdomain方法
            for(RegionEntity entity : list) {

                RegionDO domain = this.getDomain(entity);

                //判断得到的domain 是否为空 不为空 就往dolist集合里面添加domian 为空就返回空
                if(domain == null) {
                    return null;
                }
                doList.add(domain);
            }
        } else {
            return null;
        }
        return doList;
    }
}
